package com.chb.myWeather;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

/**
 * 自定义key：
 * 	  包含 year，    分区
 * 		month, 分组
 * 		T 	        温度的排序	 
 *   map输出，和reduce的输入都会将数据溢写道磁盘， 
 * 而map到reduce 会跨网络传输，所以需要持久化(序列化)
 */
public class MyKey implements WritableComparable<MyKey>{
	private int year;
	private int month;
	private double t;
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public double getT() {
		return t;
	}
	public void setT(double t) {
		this.t = t;
	}
	//反序列化
	@Override
	public void readFields(DataInput arg0) throws IOException {
		this.year = arg0.readInt();
		this.month = arg0.readInt();
		this.t = arg0.readDouble();
	}
	//序列化
	@Override
	public void write(DataOutput arg0) throws IOException {
		arg0.writeInt(year);
		arg0.writeInt(month);
		arg0.writeDouble(t);
	}
	/**
	 * 判断是否为同一个对象， 该对象最为map输出的key
	 */
	public int compareTo(MyKey o) {
		int c1 = Integer.compare(this.getYear(), o.getYear());
		if (c1 == 0) {
			int c2 = Integer.compare(this.getMonth(), o.getMonth());
			if (c2 == 0){
				return Double.compare(this.getT(), o.getT());
			}else {
				return c2;
			}
		}else {
		
			return c1;
		}
	}
	
}
